package com.back.end.advice;

import com.back.end.exception.AuthException;
import com.back.end.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
public class CommonExceptionHandler {


    public Logger logger = LoggerFactory.getLogger(CommonExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception e) {
        String msg = e.getMessage();
        logger.info(msg);
        if (msg == null || msg.equals("")) {
            msg = "服务器出错";
        }
        Result result = new Result();
        result.setCode(500);
        result.setMessage(msg);
        return result;
    }

    @ExceptionHandler(AuthException.class)
    public Result handleAuthException(AuthException e) {
        logger.error(e.getResultEnum().getMessage());
        return Result.error(e.getResultEnum());
    }


}
